{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 采集和清洗数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# from imutils import paths\n",
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['110553_12月完整数据.xlsx', '110553_11月完整数据.xlsx', '110553_22_1月完整数据.xlsx', '110553_09月完整数据.xlsx', '110553_10月完整数据.xlsx']\n"
     ]
    }
   ],
   "source": [
    "# data_list = list(paths.list_files(r'datasets/'))\n",
    "data_list = list(os.listdir(r'datasets/'))\n",
    "print(data_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(17313, 2) (17313, 2) (17313, 2) (17313, 2)\n"
     ]
    }
   ],
   "source": [
    "#读取所有的数据\n",
    "# data_list = list(paths.list_files(r'datasets/'))\n",
    "# data_list = list(os.listdir(r'datasets/'))\n",
    "data_list = [\n",
    "    '/home/hz/workplace/python/app/datasets/110553_09月完整数据.xlsx', \n",
    "    '/home/hz/workplace/python/app/datasets/110553_10月完整数据.xlsx', \n",
    "    '/home/hz/workplace/python/app/datasets/110553_11月完整数据.xlsx', \n",
    "    '/home/hz/workplace/python/app/datasets/110553_12月完整数据.xlsx', \n",
    "    '/home/hz/workplace/python/app/datasets/110553_22_1月完整数据.xlsx']\n",
    "sheet_list = ['Temperature','Humidity','illumination','s_Temperature']\n",
    "\n",
    "def load_data(data_list,sheet_name):\n",
    "    for k,i in enumerate(data_list):\n",
    "        if k==0:  #0-4\n",
    "            t = pd.read_excel(i,sheet_name=sheet_name) #读取每个sheet的数据\n",
    "        else:\n",
    "            df = pd.read_excel(i,sheet_name=sheet_name)\n",
    "            t = pd.concat([t,df],axis=0)  #按行来拼接数据\n",
    "    t = t.reset_index(drop=True) #index 重新编写\n",
    "    return t\n",
    "\n",
    "temp_df = load_data(data_list,sheet_list[0])\n",
    "humd_df = load_data(data_list,sheet_list[1])\n",
    "llum_df = load_data(data_list,sheet_list[2])\n",
    "stem_df = load_data(data_list,sheet_list[3])\n",
    "print(temp_df.shape,humd_df.shape,llum_df.shape,stem_df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>number</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>23.5</td>\n",
       "      <td>2021-09-01 00:04:54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>23.4</td>\n",
       "      <td>2021-09-01 00:15:06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23.4</td>\n",
       "      <td>2021-09-01 00:25:18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>23.3</td>\n",
       "      <td>2021-09-01 00:35:09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>23.4</td>\n",
       "      <td>2021-09-01 00:45:21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   number                date\n",
       "0    23.5 2021-09-01 00:04:54\n",
       "1    23.4 2021-09-01 00:15:06\n",
       "2    23.4 2021-09-01 00:25:18\n",
       "3    23.3 2021-09-01 00:35:09\n",
       "4    23.4 2021-09-01 00:45:21"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stem_df.head() # 查看前5行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "temp_df.to_csv('temp_df.csv',index=False)\n",
    "humd_df.to_csv('humd_df.csv',index=False)\n",
    "llum_df.to_csv('llum_df.csv',index=False)\n",
    "stem_df.to_csv('stem_df.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>number</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>17303</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2021-12-31 22:20:54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17304</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2021-12-31 22:31:02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17305</th>\n",
       "      <td>14.1</td>\n",
       "      <td>2021-12-31 22:41:10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17306</th>\n",
       "      <td>14.0</td>\n",
       "      <td>2021-12-31 22:51:17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17307</th>\n",
       "      <td>13.9</td>\n",
       "      <td>2021-12-31 23:01:25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17308</th>\n",
       "      <td>13.8</td>\n",
       "      <td>2021-12-31 23:11:32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17309</th>\n",
       "      <td>13.7</td>\n",
       "      <td>2021-12-31 23:21:40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17310</th>\n",
       "      <td>13.7</td>\n",
       "      <td>2021-12-31 23:31:48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17311</th>\n",
       "      <td>13.6</td>\n",
       "      <td>2021-12-31 23:41:55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17312</th>\n",
       "      <td>13.5</td>\n",
       "      <td>2021-12-31 23:52:03</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       number                date\n",
       "17303    14.0 2021-12-31 22:20:54\n",
       "17304    14.0 2021-12-31 22:31:02\n",
       "17305    14.1 2021-12-31 22:41:10\n",
       "17306    14.0 2021-12-31 22:51:17\n",
       "17307    13.9 2021-12-31 23:01:25\n",
       "17308    13.8 2021-12-31 23:11:32\n",
       "17309    13.7 2021-12-31 23:21:40\n",
       "17310    13.7 2021-12-31 23:31:48\n",
       "17311    13.6 2021-12-31 23:41:55\n",
       "17312    13.5 2021-12-31 23:52:03"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stem_df.tail(10) # 查看最后10行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
